<template>
  <el-cascader
    :value="value"
    size="mini"
    :props="props"
    :options="depts"
    @change="handleChange"
  />
</template>

<script>
import { getDepartments } from '@/api/department'
import { translateListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      props: {
        value: 'id', // 存储的字段 (发送给后端)
        label: 'name' // 展示的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取部门列表
    async getDepartment() {
      const res = await getDepartments()
      this.depts = translateListToTreeData(res, 0)
    },
    handleChange(list) {
      // 取到数组的最后一次
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1]) // 将最后一位的id取出来 传出去
      } else {
        this.$emit('input', null) // 如果长度为0 说明值为空
      }
    }
  }
}
</script>

  <style scoped lang='scss'>

  </style>
